// pages/index/index.js
const app = getApp()

Page({
  data: {
    showModal: false,
    currentBook: null,
    searchKeyword: '',
    categoryIndex: 0, // 当前选中的分类索引
    categories: [
      { name: "全部", value: "" },
      { name: "科学技术", value: "科学技术" },
      { name: "文学小说", value: "文学小说" },
      { name: "经济学", value: "经济学" },
      { name: "历史哲学", value: "历史哲学" }
    ],
    allBooks: [],
    filteredBooks: []
  },

  onLoad() {
    this.setData({
      allBooks: app.globalData.books,
      filteredBooks: app.globalData.books
    })
  },

  // 搜索输入处理
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    // 实时搜索
    this.filterBooks();
  },
  
  // 分类选择改变
  onCategoryChange(e) {
    this.setData({
      categoryIndex: e.detail.value
    });
    // 应用筛选
    this.filterBooks();
  },
  
  // 书籍筛选函数
  filterBooks() {
    const { searchKeyword, categories, categoryIndex, allBooks } = this.data;
    const selectedCategory = categories[categoryIndex].value;
    
    let result = allBooks;
    
    // 应用类别筛选
    if (selectedCategory) {
      result = result.filter(book => book.category === selectedCategory);
    }
    
    // 应用关键词搜索
    if (searchKeyword) {
      const keyword = searchKeyword.toLowerCase();
      result = result.filter(book => 
        book.title.toLowerCase().includes(keyword) || 
        book.author.toLowerCase().includes(keyword)
      );
    }
    
    this.setData({
      filteredBooks: result
    });
  },

  // 打开书籍详情
  openBookDetail(e) {
    const book = e.detail;
    this.setData({
      currentBook: book,
      showModal: true
    });
  },

  // 关闭模态框
  closeModal() {
    this.setData({
      showModal: false
    });
  },

  // 申领书籍
  claimBook(e) {
    const bookId = e.detail;
    wx.showModal({
      title: '提示',
      content: '书籍申领请求已提交，请等待审核确认',
      showCancel: false
    });
    
    // 更新书籍状态
    let books = this.data.allBooks;
    const bookIndex = books.findIndex(item => item.id === bookId);
    
    if (bookIndex !== -1 && books[bookIndex].status === 'available') {
      books[bookIndex].status = 'pending';
      books[bookIndex].statusText = '申领中';
      
      this.setData({
        allBooks: books
      });
      
      // 更新筛选后的列表
      this.filterBooks();
    }
  },

  // 在弹窗中申领
  claimBookInModal() {
    if (this.data.currentBook && this.data.currentBook.status === 'available') {
      const bookId = this.data.currentBook.id;
      
      // 更新数据
      let books = this.data.allBooks;
      const bookIndex = books.findIndex(item => item.id === bookId);
      
      if (bookIndex !== -1) {
        books[bookIndex].status = 'pending';
        books[bookIndex].statusText = '申领中';
        
        this.setData({
          allBooks: books,
          currentBook: books[bookIndex],
          showModal: false
        });
        
        // 更新筛选后的列表
        this.filterBooks();
      }
      
      wx.showModal({
        title: '提示',
        content: '书籍申领请求已提交，请等待审核确认',
        showCancel: false
      });
    }
  },

  // 添加到收藏夹
  addToFavorite() {
    const title = this.data.currentBook.title;
    wx.showToast({
      title: `《${title}》已添加到收藏夹`,
      icon: 'none'
    });
  }
})